import { NextRequest, NextResponse } from 'next/server'
import { db } from '@/lib/db'

export async function GET(
  request: NextRequest,
  { params }: { params: { id: string; sampleId: string } }
) {
  try {
    // 模拟获取单个样品数据
    const mockSample = {
      id: params.sampleId,
      projectId: (await params).id,
      sampleName: '测试样品',
      sampleCode: 'TS001',
      sampleType: 'material',
      description: '测试样品描述',
      quantity: 5,
      unit: '个',
      status: 'PENDING',
      location: '实验室A-1',
      receivedDate: new Date().toISOString(),
      testDate: '',
      completedDate: '',
      testResults: '',
      images: [],
      notes: '等待接收',
      createdBy: 'user-1',
      createdAt: new Date().toISOString(),
      updatedAt: new Date().toISOString()
    }
    
    return NextResponse.json(mockSample)
  } catch (error) {
    console.error('Error fetching sample:', error)
    return NextResponse.json(
      { error: 'Failed to fetch sample' },
      { status: 500 }
    )
  }
}

export async function PUT(
  request: NextRequest,
  { params }: { params: { id: string; sampleId: string } }
) {
  try {
    const body = await request.json()
    const {
      sampleName,
      sampleCode,
      sampleType,
      description,
      quantity,
      unit,
      status,
      location,
      receivedDate,
      testDate,
      completedDate,
      testResults,
      notes
    } = body
    
    // 验证必填字段
    if (!sampleName) {
      return NextResponse.json(
        { error: 'Sample name is required' },
        { status: 400 }
      )
    }
    
    // 更新样品（模拟）
    const updatedSample = {
      id: params.sampleId,
      projectId: (await params).id,
      sampleName,
      sampleCode: sampleCode || '',
      sampleType: sampleType || 'other',
      description: description || '',
      quantity: quantity || 1,
      unit: unit || '个',
      status: status || 'PENDING',
      location: location || '',
      receivedDate: receivedDate || '',
      testDate: testDate || '',
      completedDate: completedDate || '',
      testResults: testResults || '',
      images: [],
      notes: notes || '',
      createdBy: 'user-1',
      createdAt: new Date().toISOString(),
      updatedAt: new Date().toISOString()
    }
    
    console.log('Updated sample:', updatedSample)
    
    return NextResponse.json(updatedSample)
  } catch (error) {
    console.error('Error updating sample:', error)
    return NextResponse.json(
      { error: 'Failed to update sample' },
      { status: 500 }
    )
  }
}

export async function DELETE(
  request: NextRequest,
  { params }: { params: { id: string; sampleId: string } }
) {
  try {
    console.log('Deleting sample:', params.sampleId, 'from project:', (await params).id)
    
    // 删除样品（模拟）
    return NextResponse.json({ 
      message: 'Sample deleted successfully',
      sampleId: params.sampleId 
    })
  } catch (error) {
    console.error('Error deleting sample:', error)
    return NextResponse.json(
      { error: 'Failed to delete sample' },
      { status: 500 }
    )
  }
}